Ext.ns("UserSubView");
var UserSubView = function(userId, userName) {
	return new Ext.Panel(
			{
				id : "UserSubView",
				title : userName + "的下属",
				iconCls : "menu-subuser",
				autoScroll : true,
				items : [
						new Ext.FormPanel(
								{
									id : "UserSubSearchForm",
									height : 40,
									frame : false,
									border : false,
									layout : "hbox",
									layoutConfig : {
										padding : "5",
										align : "middle"
									},
									defaults : {
										xtype : "label",
										margins : {
											top : 0,
											right : 4,
											bottom : 4,
											left : 4
										}
									},
									items : [
											{
												text : "请输入查询条件:名字"
											},
											{
												xtype : "textfield",
												name : "Q_subAppUser.fullname_S_LK"
											},
											{
												xtype : "button",
												text : "查询",
												iconCls : "search",
												handler : function() {
													var a = Ext
															.getCmp("UserSubSearchForm");
													var b = Ext
															.getCmp("UserSubGrid");
													if (a.getForm().isValid()) {
														a
																.getForm()
																.submit(
																		{
																			waitMsg : "正在提交查询",
																			url : __ctxPath
																					+ "/system/listUserSub.do",
																			success : function(
																					d,
																					e) {
																				var c = Ext.util.JSON
																						.decode(e.response.responseText);
																				b
																						.getStore()
																						.loadData(
																								c);
																			}
																		});
													}
												}
											},
											{
												xtype : "button",
												text : "重置",
												iconCls : "btn-reseted",
												handler : function() {
													var a = Ext
															.getCmp("UserSubSearchForm");
													a.getForm().reset();
												}
											} ]
								}), this.setup(userId) ]
			});
};
UserSubView.prototype.setup = function(userId) {
	return this.grid(userId);
};
UserSubView.prototype.grid = function(userId) {
	var d = new Ext.grid.CheckboxSelectionModel();
	var a = new Ext.grid.ColumnModel(
			{
				columns : [
						d,
						new Ext.grid.RowNumberer(),
						{
							header : "subId",
							dataIndex : "subId",
							hidden : true
						},
						{
							header : "名字",
							dataIndex : "subAppUser",
							renderer : function(e) {
								if (e != null) {
									return e.fullname;
								} else {
									return "";
								}
							}
						},
						{
							header : "性别",
							dataIndex : "subAppUser",
							renderer : function(f) {
								var e = f.title;
								if (e == "1") {
									return "男";
								}
								if (e == "0") {
									return "女";
								}
							}
						},
						{
							header : "所在部门",
							dataIndex : "subAppUser",
							renderer : function(e) {
								if (e.department != null) {
									return e.department.depName;
								} else {
									return "";
								}
							}
						},
						{
							header : "职位",
							dataIndex : "subAppUser",
							renderer : function(e) {
								if (e.position != null) {
									return e.position;
								} else {
									return "";
								}
							}
						},
						{
							header : "电话",
							dataIndex : "subAppUser",
							renderer : function(e) {
								if (e.mobile != null) {
									return e.mobile;
								} else {
									return "";
								}
							}
						},
						{
							header : "入职时间",
							dataIndex : "subAppUser",
							renderer : function(e) {
								if (e.accessionTime != null) {
									return e.accessionTime;
								} else {
									return "";
								}
							}
						},
						{
							header : "管理",
							dataIndex : "subId",
							width : 50,
							sortable : false,
							renderer : function(h, g, e, k, f) {
								var j = e.data.subId;
								var i = '<button title="删除" value=" " class="btn-del" onclick="UserSubView.remove('
										+ j + ')">&nbsp;&nbsp;</button>';
								return i;
							}
						} ],
				defaults : {
					sortable : true,
					menuDisabled : false,
					width : 100
				}
			});
	var b = this.store(userId);
	b.load({
		params : {
			start : 0,
			limit : 25
		}
	});
	var c = new Ext.grid.GridPanel({
		id : "UserSubGrid",
		tbar : this.topbar(userId),
		store : b,
		trackMouseOver : true,
		disableSelection : false,
		loadMask : true,
		autoHeight : true,
		cm : a,
		sm : d,
		viewConfig : {
			forceFit : true,
			enableRowBody : false,
			showPreview : false
		},
		bbar : new Ext.PagingToolbar({
			pageSize : 25,
			store : b,
			displayInfo : true,
			displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
			emptyMsg : "当前没有记录"
		})
	});
	return c;
};
UserSubView.prototype.store = function(userId) {
	var a = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : __ctxPath + "/system/listUserSub.do?subId=" + userId
		}),
		reader : new Ext.data.JsonReader({
			root : "result",
			totalProperty : "totalCounts",
			id : "id",
			fields : [ {
				name : "subId",
				type : "int"
			}, "subAppUser" ]
		}),
		remoteSort : true
	});
	a.setDefaultSort("subId", "desc");
	return a;
};
UserSubView.prototype.topbar = function(userId) {
	var a = new Ext.Toolbar({
		id : "UserSubFootBar",
		height : 30,
		bodyStyle : "text-align:left",
		items : [ {
			iconCls : "btn-add",
			text : "添加下属",
			xtype : "button",
			handler : function() {
				new UserSubForm(userId);
			}
		}, {
			iconCls : "btn-del",
			text : "删除下属",
			xtype : "button",
			handler : function() {
				var d = Ext.getCmp("UserSubGrid");
				var b = d.getSelectionModel().getSelections();
				if (b.length == 0) {
					Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
					return;
				}
				var e = Array();
				for ( var c = 0; c < b.length; c++) {
					e.push(b[c].data.subId);
				}
				UserSubView.remove(e);
			}
		} ]
	});
	return a;
};
UserSubView.remove = function(b) {
	var a = Ext.getCmp("UserSubGrid");
	Ext.Msg.confirm("信息确认", "您确认要删除该记录吗？", function(c) {
		if (c == "yes") {
			Ext.Ajax.request({
				url : __ctxPath + "/system/multiDelUserSub.do",
				params : {
					ids : b
				},
				method : "post",
				success : function() {
					Ext.ux.Toast.msg("信息提示", "成功删除所选记录！");
					a.getStore().reload({
						params : {
							start : 0,
							limit : 25
						}
					});
				}
			});
		}
	});
};
UserSubView.edit = function(a) {
	new UserSubForm(a);
};